from sqlalchemy import update, select
from sqlalchemy.ext.asyncio import AsyncSession
from db.session import get_session
from model.blog import BlogCategory, Blog
from model.user import GoldCoinsInfo, GoldCoinsLog, User
from schema.response.baseResponse import CustomException


async def browse_blog_gold_coins_calculate(source_user_id: int, blog_id: int):
    """浏览博客消耗积分
    类型GoldCoinsInfo.gold_coins_type 1 是消耗积分
    Args:
        source_user_id (int): 用户id
        blog_id (int): 博客id
    """

    async with get_session() as db:
        gold_info_smts = select(GoldCoinsInfo).filter(GoldCoinsInfo.gold_coins_type == 1, GoldCoinsInfo.is_deleted == False)
        gold_coins_infos = await db.execute(gold_info_smts)
        gold_coins_info = gold_coins_infos.scalars().first()
        if not gold_coins_info:
            return CustomException(500, "积分信息不存在")
        # 添加积分记录
        gold_coins_log = GoldCoinsLog(
            source_user_id=source_user_id,
            gold_coins_info_id=gold_coins_info.id,
            blog_id=blog_id,
            text=f"浏览了博客消耗积分{gold_coins_info.gold_coins}"
        )
        
        db.add(gold_coins_log)
        await db.flush()



        # 扣除用户积分
        update_user_gold_smts = update(User).filter(User.id == source_user_id, User.is_deleted == False).values(
            gold_coins=User.gold_coins - gold_coins_info.gold_coins
        )
        await db.execute(update_user_gold_smts)

        users = await db.execute(select(User).filter(User.id == source_user_id, User.is_deleted == False))
        user = users.scalars().first()
        if user.gold_coins < 0:
            return CustomException(500, "积分不足")
        return user


async def sign_in_gold_coins_calculate(source_user_id: int, blog_id: int):
    """
    签到增加积分
    类型GoldCoinsInfo.gold_coins_type 2 是签到

    Args:
        source_user_id (int): _description_
        blog_id (int): _description_
    """
    async with get_session() as db:
        gold_info_smts = select(GoldCoinsInfo).filter(GoldCoinsInfo.gold_coins_type == 2, GoldCoinsInfo.is_deleted == False)
        gold_coins_infos = await db.execute(gold_info_smts)
        gold_coins_info = gold_coins_infos.scalars().first()
        if not gold_coins_info:
            return CustomException(500, "积分信息不存在")
        
        # 添加积分记录
        gold_coins_log = GoldCoinsLog(
            source_user_id=source_user_id,
            gold_coins_info_id=gold_coins_info.id,
            blog_id=blog_id,
            text=f"签到获得积分{gold_coins_info.gold_coins}"
        )
        
        db.add(gold_coins_log)
        await db.flush()
        # 增加用户积分
        update_user_gold_smts = update(User).filter(User.id == source_user_id, User.is_deleted == False).values(
            gold_coins=User.gold_coins + gold_coins_info.gold_coins
        )
        await db.execute(update_user_gold_smts)

async def invite_new_users_gold_coins_calculate(db, source_user_id: int, target_user_id:int, target_user_name: str, blog_id: int=None):
    """邀请新用户获得积分
    类型GoldCoinsInfo.gold_coins_type 3 是邀请新用户
    Args:
        source_user_id (int): 源用户
        target_user_id (int): 目标用户
        target_user_name (str): 目标用户名
        blog_id (int): 通过某个博客进来的
    """
    gold_info_smts = select(GoldCoinsInfo).filter(GoldCoinsInfo.gold_coins_type == 3, GoldCoinsInfo.is_deleted == False)
    gold_coins_infos = await db.execute(gold_info_smts)
    gold_coins_info = gold_coins_infos.scalars().first()
    if not gold_coins_info:
        return CustomException(500, "积分信息不存在")
    # 添加积分记录
    gold_coins_log = GoldCoinsLog(
        source_user_id=source_user_id,
        target_user_id=target_user_id,
        gold_coins_info_id=gold_coins_info.id,
        blog_id=blog_id,
        text=f"邀请新用户{target_user_name}获得积分{gold_coins_info.gold_coins}"
    )
    
    db.add(gold_coins_log)
    await db.flush()

    # 扣除用户积分
    update_user_gold_smts = update(User).filter(User.id == source_user_id, User.is_deleted == False).values(
        gold_coins=User.gold_coins + gold_coins_info.gold_coins
    )
    await db.execute(update_user_gold_smts)



    